package be.kuleuven.cs.mop.domain.model.impl;


import be.kuleuven.cs.mop.domain.exceptions.TaskManagerException;
import be.kuleuven.cs.mop.domain.model.Invitation;



/**
 * Internal implementation of the {@link Invitation} interface
 */
public class InvitationImpl implements Invitation {
	
	private InvitationStatus	status;
	private TaskImpl			task;
	private UserImpl			user;
	
	
	/**
	 * Creates a new {@link InvitationImpl}
	 * 
	 * @param task - the appropriate {@link TaskImpl}
	 * @param user - the invited {@link UserImpl}
	 * 
	 * @throws TaskManagerException
	 */
	public InvitationImpl(final TaskImpl task, final UserImpl user)
			throws TaskManagerException {
		setPending();
		setTask(task);
		setUser(user);
	}
	
	
	
	@Override
	public InvitationStatus getStatus() { return status; }
	
	@Override
	public TaskImpl getTask()  { return task; }
	
	@Override
	public UserImpl getUser() { return user; }
	
	@Override
	public boolean isAccepted() {
		return (getStatus().equals(InvitationStatus.ACCEPTED));
	}
	
	@Override
	public boolean isPending() {
		return (getStatus().equals(InvitationStatus.PENDING));
	}
	
	@Override
	public boolean isRejected() {
		return (getStatus().equals(InvitationStatus.REJECTED));
	}
	
	/**
	 * Sets the status of this {@link Invitation} to accepted
	 */
	public void setAccepted() {
		setStatus(InvitationStatus.ACCEPTED);
	}
	
	/**
	 * Sets the status of this {@link Invitation} to pending
	 */
	public void setPending() {
		setStatus(InvitationStatus.PENDING);
	}
	
	/**
	 * Sets the status of this {@link Invitation} to rejected
	 */
	public void setRejected() {
		setStatus(InvitationStatus.REJECTED);
	}
	
	private void setStatus(final InvitationStatus status) {
		if (status == null)
			throw new InternalError();
		
		this.status = status;
	}
	
	private void setTask(final TaskImpl task) throws TaskManagerException {
		if (task == null)
			throw new TaskManagerException(TaskManagerException.INVALID_TASK);
		this.task = task;
	}
	
	private void setUser(final UserImpl user) throws TaskManagerException {
		if (user == null)
			throw new TaskManagerException(TaskManagerException.INVALID_USER);
		this.user = user;
	}
	
}